<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新闻编辑</title>
    <link href="../../style/css/back/style.css" rel="stylesheet" type="text/css"/>
    <script src="../../style/js/jquery-1.8.3.js"></script>
    <script type="text/javascript" src="../../style/js/ajaxfileupload.js"></script>
    <script src="../../style/js/ckeditor/ckeditor.js"></script>
    <script src="../../style/js/ckfinder/ckfinder.js"></script>
    <script>
        var editor;
        $(function () {
            //使用ckeditor替换textArea
            editor  = CKEDITOR.replace("contentEditor");
            CKFinder.setupCKEditor(editor,'../../style/js/ckfinder')

            $.get("../../news/getNewsTypeList", function (res) {
                if (res.code === 500) {
                    alert(res.msg)
                } else if (res.code === 202) {
                    alert("登录失效")
                } else {
                    var str = "";
                    for (let i = 0; i < res.data.length; i++) {
                        str += "<option value='" + res.data[i].id + "'>" + res.data[i].name + "</option>"
                    }
                    $("#newsTypeId").append(str);
                }
            })
        })

        function save() {
            if (confirm("确认保存")){
                var title = $("#title").val()
                var newsTypeId = $("#newsTypeId").val()
                var summary = $("#summary").val()
                var content = editor.getData();

                $.ajaxFileUpload({
                    url: '../../news/addNews',
                    fileElementId: 'coverPicture',
                    dataType: 'json',
                    data: {
                        title: title,
                        newsId: newsTypeId,
                        summary: summary,
                        content: content
                    },
                    success: function (res) {
                        if (res.code===500){
                            alert(res.msg)
                        }else if (res.code===202){
                            alert("登录失效")
                        }else {
                            alert(res.msg)
                            location.replace("list.html")
                        }
                    }
                })
            }
        }

        function saveAndSubmit(){
            if (confirm("确认保存并发布")){
                var title = $("#title").val()
                var newsTypeId = $("#newsTypeId").val()
                var summary = $("#summary").val()
                var content = editor.getData();

                $.ajaxFileUpload({
                    url: '../../news/addNewsAndPush',
                    fileElementId: 'coverPicture',
                    dataType: 'json',
                    data: {
                        title: title,
                        newsId: newsTypeId,
                        summary: summary,
                        content: content
                    },
                    success: function (res) {
                        if (res.code===500){
                            alert(res.msg)
                        }else if (res.code===202){
                            alert("登录失效")
                        }else {
                            alert(res.msg)
                            location.replace("list.html")
                        }
                    }
                })
            }
        }
    </script>
</head>


<body>
<form id="formId">
    <div class="formbody">
        <div class="formtitle"><span>新闻编辑</span></div>
        <ul class="forminfo">
            <li>
                <label>新闻标题</label>
                <input name="title" id="title" type="text" class="dfinput"/>
            </li>
            <li>
                <label>新闻类型</label>
                <select id="newsTypeId" name="newsId" class="dfinput">

                </select>
            </li>
            <li>
                <label>新闻摘要</label>
                <input name="summary" id="summary" type="text" class="dfinput"/>
            </li>
            <li>
                <label>封面图</label>
                <input name="coverPicture" id="coverPicture" type="file" class="dfinput" accept=".jpg,.png"/>
            </li>
            <li>
                <label>正文</label>
                <textarea id="contentEditor">

                </textarea>
            </li>
            <li>
                <input type="button" class="btn" value="确认保存" onclick="save()"/>
                <input type="button" class="btn" value="保存并发布" onclick="saveAndSubmit()"/>
            </li>
        </ul>
    </div>
</form>
</body>
</html>